cmake_minimum_required(VERSION 3.1)
cmake_policy(SET CMP0048 NEW)
project(packet_capture VERSION 0.1.0.0)

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-unused-function")

include(ExternalProject)

# 定义 zlog 项目的编译过程
ExternalProject_Add(
    zlog_project
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/zlog-1.2.15  # 源码目录
    CONFIGURE_COMMAND ""  # 不执行配置命令
    BUILD_COMMAND make  # 执行 make 命令进行编译
    BUILD_IN_SOURCE 1  # 在源码目录中进行编译
    INSTALL_COMMAND ""  # 不执行安装命令
    BUILD_BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/libs/zlog-1.2.15/src/libzlog.a
    COMMENT "Compiling zlog library"
)

# 获取 zlog 库的路径
set(ZLOG_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/zlog-1.2.15)
set(ZLOG_LIB_NAME zlog)
set(ZLOG_LIB ${ZLOG_LIB_DIR}/src/lib${ZLOG_LIB_NAME}.a)
set (EXTRALIB ${EXTRALIB} ${ZLOG_LIB})

# 定义 libev 项目的编译过程
ExternalProject_Add(
    evlib_project
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/libev-4.33  # 源码目录
    CONFIGURE_COMMAND ./configure --enable-shared=no --enable-static=yes  # 执行配置命令
    BUILD_COMMAND make  # 执行 make 命令进行编译
    BUILD_IN_SOURCE 1  # 在源码目录中进行编译
    INSTALL_COMMAND ""  # 不执行安装命令
    BUILD_BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/libs/ibev-4.33/.libs/libev.a
    COMMENT "Compiling libev 4.33 library"
)

# 获取 libev 库的路径
set(EV_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/libev-4.33)
set(EV_LIB_NAME ev)
set(EV_LIB ${EV_LIB_DIR}/.libs/lib${EV_LIB_NAME}.a)
set (EXTRALIB ${EXTRALIB} ${EV_LIB})

# 定义 libpcap 项目的编译过程
ExternalProject_Add(
    pcaplib_project
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/libpcap-1.8.0 # 源码目录
    CONFIGURE_COMMAND ./configure --enable-shared=no --with-pcap=linux  # 执行配置命令
    BUILD_COMMAND make  # 执行 make 命令进行编译
    BUILD_IN_SOURCE 1  # 在源码目录中进行编译
    INSTALL_COMMAND ""  # 不执行安装命令
    BUILD_BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/libs/libpcap-1.8.0/libpcap.a
    COMMENT "Compiling libpcap 1.8.0 library"
)

# 获取 libpcap 库的路径
set(PCAP_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/libpcap-1.8.0)
set(PCAP_LIB_NAME pcap)
set(PCAP_LIB ${PCAP_LIB_DIR}/lib${PCAP_LIB_NAME}.a)
set (EXTRALIB ${EXTRALIB} ${PCAP_LIB})

# 定义 libjansson 项目的编译过程
ExternalProject_Add(
    janssonlib_project
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/jansson-2.14 # 源码目录
    CONFIGURE_COMMAND ./configure # 执行配置命令
    BUILD_COMMAND make  # 执行 make 命令进行编译
    BUILD_IN_SOURCE 1  # 在源码目录中进行编译
    INSTALL_COMMAND ""  # 不执行安装命令
    BUILD_BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/libs/jansson-2.14/src/.libs/libjansson.a
    COMMENT "Compiling jansson 2.14 library"
)

# 获取 libjansson库的路径
set(JANSSON_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/jansson-2.14)
set(JANSSON_LIB_NAME jansson)
set(JANSSON_LIB ${JANSSON_LIB_DIR}/src/.libs/lib${JANSSON_LIB_NAME}.a)
set (EXTRALIB ${EXTRALIB} ${JANSSON_LIB})

# options
option(DEBUG "enable debug" ON)

# include aux cmake scripts
include("cmake/Gengetopt.cmake")
ADD_GENGETOPT_FILES(CMDLINE cmdline.ggo)

include("cmake/FindMosquitto.cmake")
set (EXTRALIB ${EXTRALIB} ${MOSQUITTO_LIBRARIES})

# options
option(DEBUG "enable debug" ON)

# include aux cmake scripts
include("cmake/Gengetopt.cmake")
ADD_GENGETOPT_FILES(CMDLINE cmdline.ggo)

include("cmake/FindMosquitto.cmake")
set (EXTRALIB ${EXTRALIB} ${MOSQUITTO_LIBRARIES})


# options
option(DEBUG "enable debug" ON)

# include aux cmake scripts
include("cmake/Gengetopt.cmake")
ADD_GENGETOPT_FILES(CMDLINE cmdline.ggo)

include("cmake/FindMosquitto.cmake")
set (EXTRALIB ${EXTRALIB} ${MOSQUITTO_LIBRARIES})

#include("cmake/Findlibev.cmake")
#set (EXTRALIB ${EXTRALIB} ${LIBEV_LIBRARY})

find_package(Threads)
set (EXTRALIB ${EXTRALIB} ${CMAKE_THREAD_LIBS_INIT})

include_directories("${CMAKE_CURRENT_BINARY_DIR}"
                        "${CMAKE_SOURCE_DIR}/"
                        "${ZLOG_LIB_DIR}/src/"
                        "${EV_LIB_DIR}/"
			"${PCAP_LIB_DIR}/"
			"${JANSSON_LIB_DIR}/src/")
set(PKGC_SRC
arg.c
main.c
cJSON.c
common.c
buffer.c
packet_capture.c
socket_connect.c
)

set (STAGING_DIR $(STAGING_DIR))

# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
	"${PROJECT_SOURCE_DIR}/config.h.in"
	"${PROJECT_BINARY_DIR}/config.h"
	)

# target
add_executable(packet_capture ${CMDLINE} ${PKGC_SRC})
target_link_libraries(packet_capture ${EXTRALIB} m )
add_dependencies(packet_capture zlog_project)
add_dependencies(packet_capture evlib_project)

install(TARGETS packet_capture DESTINATION /usr/local/bin)
install(PROGRAMS files/packet_capture.sh DESTINATION /usr/local/sbin)
install(FILES files/packet_capture.ini DESTINATION /etc)
install(FILES files/p_capture DESTINATION /etc/init.d/ PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
# 自动设置 systemctl enable
install(CODE "
    execute_process(
        COMMAND systemctl daemon-reload
        RESULT_VARIABLE DAEMON_RELOAD_RESULT
        OUTPUT_VARIABLE DAEMON_RELOAD_OUTPUT
        ERROR_VARIABLE DAEMON_RELOAD_ERROR
    )
    if(NOT DAEMON_RELOAD_RESULT EQUAL 0)
        message(FATAL_ERROR \"Failed to reload systemd manager configuration: ${DAEMON_RELOAD_ERROR}\")
    endif()

    execute_process(
        COMMAND systemctl enable p_capture
        RESULT_VARIABLE ENABLE_RESULT
        OUTPUT_VARIABLE ENABLE_OUTPUT
        ERROR_VARIABLE ENABLE_ERROR
    )
    if(NOT ENABLE_RESULT EQUAL 0)
        message(FATAL_ERROR \"Failed to enable service: ${ENABLE_ERROR}\")
    endif()
")    


